package dRegAut.opg1;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Michael B. Mogensen
 */
public class O1_3 {
    public static void main(String[] args) {
        FA unionFA = new O1_3().setupM1().union(new O1_3().setupM2());
        FA intersectionFA = new O1_3().setupM1().intersection(new O1_3().setupM2());
        FA minusFA = new O1_3().setupM1().minus(new O1_3().setupM2());

        unionFA.checkWellDefined();
        intersectionFA.checkWellDefined();
        minusFA.checkWellDefined();

        try {
            createToDotFile(unionFA.toDot(), "union.dot");
            createToDotFile(intersectionFA.toDot(), "intersection.dot");
            createToDotFile(unionFA.toDot(), "minus.dot");
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Accept 1101 " + unionFA.accepts("1101"));
        System.out.println("Accept 101001 " + unionFA.accepts("101001"));

        System.out.println("Accept empty string " + unionFA.accepts(""));
        System.out.println("Accept 1010 " + unionFA.accepts("1010"));


    }

    private FA setupM1() {
        FA m1FA = new FA();
        State stateA = new State("a");
        State stateB = new State("b");
        State stateC = new State("c");
        State stateD = new State("d");

        m1FA.alphabet = new Alphabet("01");

        m1FA.states.add(stateA);
        m1FA.states.add(stateB);
        m1FA.states.add(stateC);
        m1FA.states.add(stateD);

        // Set up graph M1, initial state=a, accepted states=c, d
        m1FA.initial = stateA;
        m1FA.accept.add(stateC);
        m1FA.accept.add(stateD);

        m1FA.setTransition(stateA, '0', stateB);
        m1FA.setTransition(stateA, '1', stateA);

        m1FA.setTransition(stateB, '0', stateC);
        m1FA.setTransition(stateB, '1', stateD);

        m1FA.setTransition(stateC, '0', stateC);
        m1FA.setTransition(stateC, '1', stateD);

        m1FA.setTransition(stateD, '0', stateB);
        m1FA.setTransition(stateD, '1', stateA);

        return m1FA;
    }

    private FA setupM2() {
        FA m2FA = new FA();
        State stateA = new State("a");
        State stateB = new State("b");
        State stateC = new State("c");

        m2FA.alphabet = new Alphabet("01");

        m2FA.states.add(stateA);
        m2FA.states.add(stateB);
        m2FA.states.add(stateC);

        // Set up graph M2, initial state=a, accepted states=c
        m2FA.initial = stateA;
        m2FA.accept.add(stateC);

        m2FA.setTransition(stateA, '0', stateA);
        m2FA.setTransition(stateA, '1', stateB);

        m2FA.setTransition(stateB, '0', stateA);
        m2FA.setTransition(stateB, '1', stateC);

        m2FA.setTransition(stateC, '0', stateA);
        m2FA.setTransition(stateC, '1', stateC);

        return m2FA;
    }

    public static void createToDotFile(String toDot, String file) throws IOException {
        FileWriter out = new FileWriter(file);
        out.write(toDot);
        out.close();
    }
}